define(['moment'], function () {
    return ['$scope', '$rootScope', '$filter', '$stateParams', '$mdDialog', 'config', 'MMWService', '$calculater',
        function ($scope, $rootScope, $filter, $stateParams, $mdDialog, config, $MMWService, $calculater) {

            $scope.init = function () {
                $MMWService.PNIGList = {};
                $scope.warehouseInfo = {
                    type: '', product_warehouse: '', storage: '',
                    plot_info_detail: [],
                    wo_no: '', item_no: '', plot_no: '', erp_doc_type: ''
                };

                $scope.type = '0';
                $scope.selectedList = [];
                $scope.work_type = "1";//1 入庫，0 合併轉移

                $scope.warehouseInfo.item_no = '';
                $scope.warehouseInfo.item_name = '';
                $scope.warehouseInfo.wo_no = '';

                $scope.warehouseInfo.select_type = 3;

                $scope.list_filter = {};
                $scope.filterSH = {};
                $scope.do_flag = 1;
            };

            $scope.back = function () {
                $scope.$root.hideMenu();
                $scope.$root.changeProgram('home', true);
            };

            $scope.chengeType = function (type) {
                $scope.work_type = type;
                $scope.selectedList.forEach(function (item) {
                    item.actual_prd_no = item.item_no;
                    item.actual_prd_name = item.item_name;
                    item.actual_prd_ver = item.item_ver;
                    item.actual_prd_unit = item.item_unit_no;
                    $scope.warehouseInfo.plot_info_detail.push(item);
                });
                $scope.warehouseInfo.plot_info_detail =
                    $filter('orderBy')($scope.warehouseInfo.plot_info_detail, ['item_no', 'item_ver', 'op_no', 'lot_no']);
                $scope.selectedList = [];
            }
            //搜尋資料
            $scope.search = function () {
                $scope.warehouseInfo.plot_info_detail = [];
                $scope.warehouseInfo.type = '';
                $scope.warehouseInfo.product_warehouse = '';
                $scope.warehouseInfo.storage = '';
                $scope.warehouseInfo.plot_no = '';
                $scope.warehouseInfo.erp_doc_type = '';
                $scope.selectedList = [];
                var content = {
                    wo_no: $scope.warehouseInfo.wo_no,
                    item_no: $scope.warehouseInfo.item_no
                };
                warehousePlotInfoGet(content, function (response) {
                    $scope.list_filter = {};
                    $scope.filterSH = {};
                    if (response && response.length > 0) {
                        $scope.warehouseInfo.plot_info_detail = response;
                        $scope.warehouseInfo.plot_info_detail.forEach(function (detail) {
                            detail.lot_code = detail.plot_no;
                            detail.checked = false;
                            detail.actual_prd_no = detail.item_no;
                            detail.actual_prd_name = detail.item_name;
                            detail.actual_prd_ver = detail.item_ver;
                            detail.actual_prd_unit = detail.item_unit_no;
                            detail.joint_trigger = 'N';
                            detail.do_flag = $scope.do_flag;
                        });
                    } else {
                        $scope.warehouseInfo.plot_info_detail = [];
                        $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                    }
                });
            };
            $scope.confirm = function () {
                var worktype = $scope.work_type;
                if ($scope.selectedList.length <= 0)
                    return;
                if (worktype == 1) {
                    //入庫
                    var sameInventoryAndStorage = isCheckSameInventoryAndStorage($scope.selectedList);
                    $scope.openStorage(sameInventoryAndStorage);
                }
                else if (worktype == 0) {
                    //合併
                    $scope.openMerge();
                }

            };

            // 20200917 modify by WeiTing for M#72157 - 檢查選取的資料是否都是相同的倉庫和儲位。
            function isCheckSameInventoryAndStorage(selectedList) {
                var checkList = {
                    inventoryNo: '', // 倉庫
                    storageSpacesNo: '', // 儲位
                    isStopInventory: false, // 不同倉庫時，停止判斷
                    isStopStorage: false // 不同儲位時，停止判斷
                };

                if (selectedList && selectedList.length > 1) {
                    for (var i = 0; i < selectedList.length > 0; i++) {
                        if (i < selectedList.length - 1) {
                            // 檢查選取的資料的倉庫是否都相同
                            if (selectedList[i].inventoryno && !checkList.isStopInventory) {
                                if (selectedList[i].inventoryno === selectedList[i + 1].inventoryno) {
                                    checkList.inventoryNo = selectedList[i].inventoryno;
                                } else {
                                    checkList.inventoryNo = '';
                                    checkList.isStopInventory = true;
                                }
                            } else {
                                checkList.inventoryNo = '';
                                checkList.isStopInventory = true;
                            }

                            // 檢查選取的資料的儲位是否都相同
                            if (selectedList[i].storage_spaces_no && !checkList.isStopStorage) {
                                if (selectedList[i].storage_spaces_no === selectedList[i + 1].storage_spaces_no) {
                                    checkList.storageSpacesNo = selectedList[i].storage_spaces_no;
                                } else {
                                    checkList.storageSpacesNo = '';
                                    checkList.isStopStorage = true;
                                }
                            } else {
                                checkList.storageSpacesNo = '';
                                checkList.isStopStorage = true;
                            }
                        }
                    }
                } else {
                    if (selectedList[0].inventoryno) {
                        checkList.inventoryNo = selectedList[0].inventoryno;
                    }
                    if (selectedList[0].storage_spaces_no) {
                        checkList.storageSpacesNo = selectedList[0].storage_spaces_no;
                    }
                }

                return checkList;
            }
            //取得產品編號
            $scope.loadItemNo = function () {
                /*var page_mode;
                //20170704 modify by Dustdusk for 入庫產品清單查詢優化
                if($scope.type == '0'){
                    page_mode = 18;
                } else if($scope.type == '1'){
                    page_mode = 19;
                }*/
                // 20230314 modify by Alan for#129867 : 調整成新版參數
                $MMWService.showQueryListPNIG({
                    form_name: 'PageNo_10',
                    pageRecord: 50,
                    key: 'page_no',
                    condition: [
                        { name: $filter('translate')('common.queryList.item_c'), column_name: 'B.ProductNo', query_mode: '2', value: $scope.warehouseInfo.item_no || '' },
                        { name: $filter('translate')('common.queryList.item_n'), column_name: 'B.ProductName', query_mode: '2', value: '' }
                    ],
                    header: [
                        { label: $filter('translate')('common.queryList.item_c'), code: 'page_no' },
                        { label: $filter('translate')('common.queryList.item_n'), code: 'page_name' }
                    ],
                    selectCode: $scope.warehouseInfo.item_no,
                    confirm: function (selectItem) {
                        $scope.warehouseInfo.item_no = selectItem.page_no;
                        $scope.warehouseInfo.item_name = selectItem.page_name;
                        $scope.warehouseInfo.wo_no = '';
                    }
                });
            };

            //取得工單編號
            $scope.loadWoNo = function () {
                //var page_mode;
                //20170704 modify by Dustdusk for 入庫工單清單查詢優化
                /*if($scope.type == '0'){
                    page_mode = 20;
                } else if($scope.type == '1'){
                    page_mode = 21;
                }*/
                // 20230314 modify by Alan for#129867 : 調整成新版參數
                $MMWService.showQueryListPNIG({
                    form_name: 'PageNo_14',
                    pageRecord: 50,
                    condition: [
                        { name: $filter('translate')('common.queryList.wo_c'), column_name: 'C.MoNo', query_mode: '2', value: $scope.warehouseInfo.wo_no }
                    ],
                    header: [
                        { label: $filter('translate')('common.queryList.wo_c'), code: 'page_no' }
                    ],
                    setCondition: function (conditions) {
                        if ($scope.warehouseInfo.item_no && $scope.warehouseInfo.item_no != '')
                            conditions.push({ column_name: 'B.ProductNo', query_mode: '0', value: $scope.warehouseInfo.item_no, merge_condition_model: 'AND' });

                        return conditions;
                    },
                    selectCode: $scope.warehouseInfo.wo_no,
                    confirm: function (selectItem) {
                        $scope.warehouseInfo.wo_no = selectItem.page_no;
                    }
                });
            };

            $scope.pickTarget = function () {
                var targetArray = [];
                for (var i = 0; i < $scope.warehouseInfo.plot_info_detail.length; i++) {
                    var item = $scope.warehouseInfo.plot_info_detail[i];
                    var lot_no = item.plot_no;
                    //console.log($scope.warehouseInfo.plot_no+":"+lot_no+":"+lot_no.indexOf($scope.warehouseInfo.plot_no));
                    if (lot_no.indexOf($scope.warehouseInfo.plot_no) >= 0) {
                        var push_bool = true;
                        if ($scope.work_type == "0") {
                            var same_process_condition = {};
                            same_process_condition.item_no = item.item_no;
                            same_process_condition.item_ver = item.item_ver;
                            same_process_condition.process_no = item.process_no;
                            same_process_condition.process_ver = item.process_ver;
                            same_process_condition.next_op_no = item.next_op_no;
                            var has_same_process_data = $filter('filter')($scope.selectedList, same_process_condition);
                            if ($scope.selectedList.length > 0) {
                                if (has_same_process_data && has_same_process_data.length == 0) {
                                    push_bool = false;
                                }
                            }
                        }
                        if (push_bool) {
                            var check_condition = {}
                            check_condition.index = i;
                            check_condition.plot_no = item.plot_no;
                            check_condition.op_no = item.op_no;
                            check_condition.wo_no = item.wo_no;
                            check_condition.process_no = item.process_no;
                            check_condition.process_ver = item.process_ver;
                            var has_data = $filter('filter')($scope.selectedList, check_condition);
                            if (has_data && has_data.length <= 0)
                                targetArray.push(i);
                        }
                    }

                }
                //push select
                targetArray.forEach(function (target) {
                    var item = $scope.warehouseInfo.plot_info_detail[target];
                    $scope.selectedList.push(item);
                });
                //remove plot_info_detail(index desc for remove)
                targetArray = targetArray.sort(function (a, b) { return b - a });
                targetArray.forEach(function (target) {
                    $scope.warehouseInfo.plot_info_detail.splice(target, 1);
                });
            };
            $scope.returnTarget = function (index, item) {
                var has_data = $filter('filter')($scope.selectedList, check_condition);
                var check_condition = {}
                check_condition.plot_no = item.plot_no;
                check_condition.op_no = item.op_no;
                check_condition.wo_no = item.wo_no;
                check_condition.process_no = item.process_no;
                check_condition.process_ver = item.process_ver;
                var has_data = $filter('filter')($scope.warehouseInfo.plot_info_detail, check_condition);
                if (has_data && has_data.length > 0) {
                    $scope.$root.showAlert($filter('translate')('B1301.msg.repeat'));
                }
                else {
                    $scope.warehouseInfo.plot_info_detail.push(item);
                    $scope.selectedList.splice(index, 1);
                    $scope.warehouseInfo.plot_info_detail =
                        $filter('orderBy')($scope.warehouseInfo.plot_info_detail, ['item_no', 'item_ver', 'op_no', 'lot_no']);

                }
            };
            $scope.itemCheck = function (index, item) {

                if ($scope.work_type == "0") {
                    if (item.next_op_no == "END") {
                        $scope.$root.showAlert($filter('translate')('B1301.msg.next_op_is_end'));
                        return;
                    }
                    var same_process_condition = {};
                    same_process_condition.item_no = item.item_no;
                    same_process_condition.item_ver = item.item_ver;
                    same_process_condition.process_no = item.process_no;
                    same_process_condition.process_ver = item.process_ver;
                    same_process_condition.next_op_no = item.next_op_no;
                    var has_same_process_data = $filter('filter')($scope.selectedList, same_process_condition);
                    if ($scope.selectedList.length > 0) {
                        if (has_same_process_data && has_same_process_data.length == 0) {
                            $scope.$root.showAlert($filter('translate')('B1301.msg.diff_product'));
                            return;
                        }
                    }
                }
                item.index = index;
                var check_condition = {}
                check_condition.index = index;
                check_condition.plot_no = item.plot_no;
                check_condition.op_no = item.op_no;
                check_condition.wo_no = item.wo_no;
                check_condition.process_no = item.process_no;
                check_condition.process_ver = item.process_ver;
                var has_data = $filter('filter')($scope.selectedList, check_condition);
                if (has_data && has_data.length > 0) {
                    $scope.$root.showAlert($filter('translate')('B1301.msg.repeat'));
                }
                else {
                    $scope.selectedList.push(item);
                    $scope.warehouseInfo.plot_info_detail.splice(index, 1);
                }
            };

            $scope.openMerge = function () {
                $mdDialog.dialog('module/moduleB/program/B13/B1301/B1301_MergePlot.tmp.html', mergeController);
            }
            function mergeController(dialog) {
                var controller = {
                    lot_info: $scope.selectedList,
                    mode: "0",
                    target_op: "",
                    target_plot: "",
                    product_no: "",
                    product_ver: "",
                    transferQty: 0,
                    init: function () {
                        dialog.target_op_no = dialog.lot_info[0].next_op_no;
                        dialog.target_plot = dialog.lot_info[0].plot_no;
                        dialog.product_no = dialog.lot_info[0].item_no;
                        dialog.product_ver = dialog.lot_info[0].item_ver;
                        dialog.lot_info.forEach(function (item) {
                            item.limit_qty = (+item.plot_qty);
                        });
                    },
                    loadlotinfo: function () {
                        $scope.$root.showSelect({
                            title: $filter('translate')('B1301.otherTitle.column1'),
                            label: function (item) {
                                return item.plot_no;
                            },
                            code: 'plot_no',
                            selectCode: dialog.target_plot,
                            list: dialog.lot_info,
                            confirm: function (item, select_dialog) {
                                select_dialog.hide();
                                dialog.target_plot = item.plot_no;
                            }
                        });
                    },
                    openTransferQty: function (target) {
                        $calculater.open(
                            function (num, c_dialog) {
                                angular.element(target).removeClass('is-focus');
                                c_dialog.hide();
                                dialog.transferQty = num; //將數字回填給原本的物件
                            },
                            function (c_dialog) {
                                angular.element(target).removeClass('is-focus');
                                c_dialog.hide();
                            },
                            {
                                defaultNum: dialog.transferQty,
                                decimal: false
                            }
                        );
                    },
                    openLotQty: function (target, item) {
                        $calculater.open(
                            function (num, c_dialog) {
                                angular.element(target).removeClass('is-focus');
                                c_dialog.hide();
                                if (num <= item.limit_qty)
                                    item.plot_qty = num; //將數字回填給原本的物件
                                else
                                    $scope.$root.showAlert($filter('translate')('B1301.msg.qty_over'));
                            },
                            function (c_dialog) {
                                angular.element(target).removeClass('is-focus');
                                c_dialog.hide();
                            },
                            {
                                defaultNum: item.plot_qty,
                                decimal: false
                            }
                        );
                    },
                    changemode: function (type) {
                        dialog.mode = String(type);
                    },
                    back: function () {
                        dialog.hide();
                    },
                    confirm: function () {
                        var check_lot = [];
                        var error_msg = [];
                        if (dialog.transferQty <= 0) {
                            $scope.$root.showAlert($filter('translate')('B1301.msg.transfer_qty_less'));
                            return;
                        }
                        for (var i in dialog.lot_info) {
                            var item = dialog.lot_info[i];
                            var index = check_lot.findIndex(a => a.op_no == item.op_no);
                            if (index >= 0) {
                                check_lot[index].qty += (+item.plot_qty);
                            }
                            else {
                                var obj = {};
                                obj.op_no = item.op_no;
                                obj.qty = (+item.plot_qty);
                                check_lot.push(obj);
                            }
                        }
                        // 20210406 modify by 奕達 for M#90332 人工扣減不卡控，0:系統輔助扣減 1:人工控制扣減
                        if (dialog.mode === "0") {
                            for (var i in check_lot) {
                                if (dialog.transferQty > check_lot[i].qty) {
                                    var msg = $filter('translate')('B1301.msg.transfer_over_1') + check_lot[i].op_no + $filter('translate')('B1301.msg.transfer_over_2');
                                    error_msg.push(msg);
                                }
                            }
                        }
                        if (error_msg.length > 0) {
                            $scope.$root.showAlert(error_msg.join("<br>"));
                            return;
                        }
                        // 20210401 modify by 奕達 for M#90336
                        var content = {};
                        content.product_no = dialog.product_no;
                        content.product_version = dialog.product_ver;
                        content.to_lot_no = dialog.target_plot;
                        content.to_op_no = dialog.target_op_no;
                        content.transfer_qty = dialog.transferQty;
                        content.deduction_mode = dialog.mode;
                        content.process_no = dialog.lot_info[0].process_no;
                        content.process_ver = dialog.lot_info[0].process_ver;
                        content.next_op_node_id = dialog.lot_info[0].next_op_node_id;
                        content.list = dialog.lot_info;
                        $MMWService.sendToServer({
                            uri: 'ServicesSTD.Module_WIP.mesh_stuck_transfer_process',
                            content: content,
                            success: function (response) {
                                $scope.$root.showToast($filter('translate')('B1301.msg.process_success'));
                                dialog.hide();
                                $scope.init();
                            }
                        });
                    }
                };
                return controller;
            }
            $scope.openStorage = function (sameInventoryAndStorage) {
                $scope.sameInventoryAndStorage = sameInventoryAndStorage;
                $mdDialog.dialog(
                    'module/moduleB/program/B13/B1301/B1301_SearchPlot.tmp.html',
                    storageController
                );
            };

            function storageController(dialog) {
                var controller = {
                    warehouseInfo: $scope.warehouseInfo,
                    //成品庫
                    init: function () {
                        // 20200917 modify by WeiTing for M#72157 - 如果選取的資料的倉庫和儲位都相同時，判斷是否跟下拉式選單的資料符合，符合就預設為此倉庫和儲位。
                        var conditionvalue = 1;
                        // 良品 = 1 , 不良品 =3
                        /*if($scope.type == 1){
                            conditionvalue = 3;
                        } else {
                            conditionvalue = 1;
                        }*/
                        // 20230314 modify by Alan for#129867 : 調整成新版參數
                        pageNumberInfoGet({
                            // 倉庫
                            form_name: 'PageNo_6',
                            query_condition_info: [
                                { column_name: 'InventoryType', query_mode: '0', value: conditionvalue }
                            ]
                        }, function (response) {
                            if (response && response.length > 0) {
                                for (var i = 0; i < response.length; i++) {
                                    // 判斷倉庫是否跟下拉式選單的資料符合
                                    if (response[i].page_no === $scope.sameInventoryAndStorage.inventoryNo) {
                                        $scope.warehouseInfo.product_warehouse_name = response[i].page_name;
                                        $scope.warehouseInfo.product_warehouse = response[i].page_no;

                                        $scope.warehouseInfo.storage = '';
                                        $scope.warehouseInfo.storage_display = '';
                                        break;
                                    }
                                }
                            }
                            // 20230314 modify by Alan for#129867 : 調整成新版參數
                            pageNumberInfoGet({
                                // 儲位
                                form_name: 'PageNo_7',
                                query_condition_info: [
                                    { column_name: 'InventoryNo', query_mode: '0', value: $scope.warehouseInfo.product_warehouse }
                                ]
                            }, function (response) {
                                if (response && response.length > 0) {
                                    for (var i = 0; i < response.length; i++) {
                                        // 判斷儲位是否跟下拉式選單的資料符合
                                        if (response[i].page_no === $scope.sameInventoryAndStorage.storageSpacesNo) {
                                            $scope.warehouseInfo.storage = response[i].page_no;
                                            $scope.warehouseInfo.storage_display = response[i].page_name;
                                            break;
                                        }
                                    }
                                }
                            });
                        });
                    },
                    loadProductWarehouse: function () {
                        // 良品 = 1 , 不良品 =3
                        var conditionvalue = 1;
                        /*if($scope.type == 1){
                            conditionvalue = 3;
                        } else {
                            conditionvalue = 1;
                        }*/
                        // 20230314 modify by Alan for#129867 : 調整成新版參數
                        pageNumberInfoGet({
                            form_name: 'PageNo_6',
                            query_condition_info: [
                                { column_name: 'InventoryType', query_mode: '0', value: conditionvalue }
                            ]
                        }, function (response) {
                            $scope.$root.showSelect({
                                title: $filter('translate')('B1301.otherTitle.column1'),
                                label: function (item) {
                                    return item.page_no + ' (' + item.page_name + ')';
                                },
                                code: 'page_no',
                                selectCode: $scope.warehouseInfo.product_warehouse,
                                list: response,
                                confirm: function (item, dialog) {
                                    dialog.hide();
                                    $scope.warehouseInfo.product_warehouse_name = item.page_name;
                                    $scope.warehouseInfo.product_warehouse = item.page_no;

                                    $scope.warehouseInfo.storage = '';
                                    $scope.warehouseInfo.storage_display = '';
                                }
                            });
                        });
                    },
                    //儲位
                    loadStorage: function () {
                        if ($scope.warehouseInfo.product_warehouse != '') {
                        // 20230314 modify by Alan for#129867 : 調整成新版參數
                        pageNumberInfoGet({
                                form_name: 'PageNo_7',
                                query_condition_info: [
                                    { column_name: 'InventoryNo', query_mode: '0', value: $scope.warehouseInfo.product_warehouse }
                                ]
                            }, function (response) {
                                $scope.$root.showSelect({
                                    title: $filter('translate')('B1301.otherTitle.column1'),
                                    label: function (item) {
                                        return item.page_no + ' (' + item.page_name + ')';
                                    },
                                    code: 'page_no',
                                    selectCode: $scope.warehouseInfo.storage,
                                    list: response,
                                    confirm: function (item, dialog) {
                                        dialog.hide();
                                        $scope.warehouseInfo.storage = item.page_no;
                                        $scope.warehouseInfo.storage_display = item.page_name;
                                    }
                                });
                            });
                        } else {
                            $scope.$root.showAlert($filter('translate')('B1301.msg.product_warehouse_empty'));
                        }

                    },
                    loadERP: function () {
                        $rootScope.showQueryList({
                            pageRecord: 50,
                            header: [
                                { label: $filter('translate')('B0216.queryList.column3_1'), code: 'page_no' },
                                { label: $filter('translate')('B0216.queryList.column3_2'), code: 'page_name' }
                            ],
                            query: function (conditions, feedback) {
                                $MMWService.sendToServer({
                                    uri: 'ServicesSTD.Module_SYS.erp_document_type_get',
                                    content: { doc_type: $scope.do_flag == 0 ? '1' : '6' },
                                    success: function (data) {
                                        feedback(data);
                                    }
                                });
                            },
                            selectCode: $scope.warehouseInfo.erp_doc_type,
                            confirm: function (selectItem, dialog) {
                                $scope.warehouseInfo.erp_doc_type = selectItem.page_no;
                                dialog.hide();
                            }
                        });
                    },
                    back: function () {
                        $scope.warehouseInfo.product_warehouse_name = '';
                        $scope.warehouseInfo.product_warehouse = '';
                        $scope.warehouseInfo.storage = '';
                        $scope.warehouseInfo.storage_display = '';
                        $scope.warehouseInfo.erp_doc_type = '';
                        dialog.hide();
                    },
                    confirm: function () {
                        if ($scope.warehouseInfo.product_warehouse.length == 0) {
                            $rootScope.showAlert($filter('translate')('B1301.showAlert.column3'));
                        }
                        else {
                            dialog.hide();
                            warehousePlotProcess();
                        }
                        /*if($scope.type==0){
                        } else if($scope.type == 1){
                            dialog.hide();
                            warehousePlotProcess();
                        }*/
                    }
                }
                return controller;
            }

            //取得單號
            // 1 工單編號 6產品編號 13成品庫 14儲位
            function pageNumberInfoGet(content, feedback) {
                $MMWService.pageNumberInfoGet(
                    content
                    , function (itemList) {
                        feedback(itemList);
                    });
            }


            //取得待入庫資料
            //type 0:良品  1:不良品
            function warehousePlotInfoGet(content, feedback) {
                $MMWService.sendToServer({
                    uri: 'ServicesSTD.Module_WIP.mesh_stuck_info_get',
                    content: content,
                    success: function (data) {
                        feedback(data.mesh_stuck_info_get);
                    }
                });
            }

            //送出確定的待入庫資料
            //type 0:良品 1:不良品
            function warehousePlotProcess() {
                var msg = '';
                var content = {
                    type: 5,
                    product_warehouse: $scope.warehouseInfo.product_warehouse,
                    storage: $scope.warehouseInfo.storage,
                    erp_doc_type: $scope.warehouseInfo.erp_doc_type,
                    do_flag: $scope.do_flag,
                    plot_info_detail: $scope.selectedList
                };
                $MMWService.sendToServer({
                    uri: 'ServicesSTD.Module_WIP.warehouse_plot_process',
                    content: content,
                    success: function (data, response) {
                        //20180514 modify by Dustdusk for M#46940:入庫完成的訊息由後台提供 請抓 response.Message
                        var msg = $filter('translate')('B1301.msg.process_success');
                        if (response.Message != undefined && response.Message != '') {
                            msg = response.Message;
                        }
                        //20170330 modify by megabytes for 確認存取完後清空
                        $scope.warehouseInfo.item_name = '';
                        $scope.warehouseInfo.plot_no = '';
                        $scope.selectedList = [];

                        // 20200902 modify by WeiTing for M#74338: 增加系統參數(WarehouseShowERPNo)來判斷入庫單號是否顯示確認跳窗，讓使用者記錄完後，自行關閉。
                        if (config.cache.sys_parameter.WarehouseShowERPNo === 'Y') {
                            $scope.$root.showConfirm(msg, function (confirm_dialog) {
                                confirm_dialog.hide();
                            });
                        } else {
                            $scope.$root.showToast(msg, 10);
                        }

                        $scope.search();
                    }
                });
            }

            $scope.changeSelectType = function (type) {
                if ($scope.warehouseInfo.select_type == 4 && type != 4) {
                    $scope.selectedList.forEach(function (selectItem) {
                        selectItem.cancel_qty = undefined;
                    });
                }
                $scope.warehouseInfo.select_type = type;
            };
            $scope.product_change = function (event, item) {
                $mdDialog.serviceDialog('B0223Service', {
                    parent: 'B1301',
                    plotItem: item,
                    do_flag: 0,
                    close: function (newItem) {
                    },
                    feedback: function (newItem) {
                        item.actual_prd_no = newItem.joint_item_no;
                        item.actual_prd_name = newItem.joint_item_name;
                        item.actual_prd_unit = newItem.joint_item_unit_no;
                        item.actual_prd_ver = newItem.joint_item_ver;
                        if (item.actual_prd_no != item.item_no)
                            item.joint_trigger = 'Y';
                    }
                });
            };

            $scope.recombine = function (event, item) {
                $mdDialog.serviceDialog('B1303Service', {
                    parent: 'B1301',
                    plotItem: item,
                    do_flag: $scope.do_flag,
                    close: function (newItem) {
                        item.item_no = newItem.item_no;
                        item.item_name = newItem.item_name;
                        item.item_unit_no = newItem.item_unit_no;
                        item.item_ver = newItem.item_ver;
                    },
                    feedback: function (newItem) {
                        item.old_item_no = item.item_no;
                        item.old_item_name = item.item_name;
                        item.old_item_unit_no = item.item_unit_no;
                        item.old_item_ver = item.item_ver;

                        item.joint_item_no = item.item_no = newItem.joint_item_no;
                        item.item_name = newItem.joint_item_name;
                        item.joint_item_unit_no = item.item_unit_no = newItem.joint_item_unit_no;
                        item.item_ver = newItem.joint_item_ver;
                        item.joint_trigger = 'Y';
                    }
                });
            };
        }
    ];
});
